package com.base.chat.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.chat.R;
import com.base.chat.adapter.MyFriendListAdapter;
import com.base.chat.bean.MyFriendsBean;
import com.base.chat.databinding.FragmentMyFriendsBinding;
import com.base.chat.presenter.MyFriendsListPresenter;
import com.base.chat.util.ChatStatEntity;
import com.base.chat.view.MyFriendsListView;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 我的好友列表
 */
public class MyFriendListFragment extends CBaseMvpFragment<MyFriendsListPresenter>implements MyFriendsListView {
    private FragmentMyFriendsBinding mBinding;
    private Disposable disposable;
    private MyFriendListAdapter mMyFriendListAdapter;
    private List<MyFriendsBean.Rows> mList=new ArrayList<>();

    @Override
    public void init() {
        mBinding = (FragmentMyFriendsBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter =new MyFriendsListPresenter(this,getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }

    @Override
    public void start() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(linearLayoutManager);
        mPresenter.getList("",1,10);
        mBinding.imgRight.setBackgroundResource(R.mipmap.icon_add_friend);
        mBinding.imgRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/friend/add").navigation();
            }
        });
        mBinding.title.setText("好友");
        mBinding.bar.setNavigationIcon(R.drawable.ic_title_back);
        mBinding.bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        mMyFriendListAdapter= new MyFriendListAdapter(mContext,getActivity(),mList);
        mMyFriendListAdapter.isHead(0);
        disposable = RxBus2.getInstance().doSubscribe(ChatStatEntity.class, new Consumer<ChatStatEntity>() {
            @Override
            public void accept(ChatStatEntity bean) throws Exception {
                if (bean.getState() == 0X0000010) {
                    if (mPresenter!=null){
                       // mPresenter.getList(bean.getData());
                    }
                }
            }
        });

        mBinding.rv.setAdapter(mMyFriendListAdapter);

//        mBinding.rv.addItemDecoration(new RecyclerViewSpacesItemDecoration
//                (10, 10, 10, 10));
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my_friends;
    }


    @Override
    public void returnNoData() {

    }

    @Override
    public void returnList(MyFriendsBean bean) {
     //   mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mList.clear();
        mList.addAll(bean.getRows());
        mMyFriendListAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(MyFriendsBean bean) {
       // mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mList.addAll(bean
                .getRows());
        if (mMyFriendListAdapter != null) {
            mMyFriendListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnNoMoreData() {

    }

    int page =1;

    @Override
    public void returnError() {
        //mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void returnAddFriendSuccess(String msg) {
        ToastUtil.show(msg);
    }
}
